package com.ruipengkj.model;

import java.util.ArrayList;
import java.util.List;
import java.util.Vector;

import javax.swing.table.AbstractTableModel;

import com.ruipengkj.bean.TCommodity;
import com.ruipengkj.bean.TStockAlarm;
import com.ruipengkj.commons.util.MatcherFormatUtil;

public class StockAlarmAddModel extends AbstractTableModel {

	private static final long serialVersionUID = 1L;
	private List<TStockAlarm> tStockAlarmList=new ArrayList<TStockAlarm>();
	private Vector<String> columns = new Vector<String>();
	
	public StockAlarmAddModel(){
		columns.add("报警编号");
		columns.add("商品编号");
		columns.add("商品名称");
		columns.add("最低阀值");
		columns.add("最高阀值");
	}
	
	//获得行数
	public int getRowCount(){
		return tStockAlarmList.size();
	}
	//获得列数
	public int getColumnCount(){
		return columns.size();
	}
	//获得单元格的值
	public Object getValueAt(int row,int column){
		TStockAlarm tStockAlarm = tStockAlarmList.get(row);
		return getTStockAlarmValueByCol(tStockAlarm, column);
	}
	
	public Object getTStockAlarmValueByCol(TStockAlarm tStockAlarm, int column) {
		switch (column) {
			case 0:
				return tStockAlarm.getAlarmID();
			case 1:
				TCommodity commodity=tStockAlarm.getTCommodity();
				if(commodity!=null){
					return commodity.getCommodityID();
				}else{
					return "";
				}
			case 2:
				TCommodity commodity2=tStockAlarm.getTCommodity();
				if(commodity2!=null && commodity2.getName() != null){
					return commodity2.getName();
				}else{
					return "";
				}
			case 3:
				return tStockAlarm.getMinWalve();
			case 4:
				return tStockAlarm.getMaxWalve();
		}
		return null;
	}
	
	//获取列名
	public String getColumnName(int column){
		return (String)columns.get(column);
	}
	
	public void addList(TStockAlarm tStockAlarm) {
		tStockAlarmList.add(tStockAlarm);
		fireTableRowsInserted(tStockAlarmList.size()-1, tStockAlarmList.size()-1);
		
	}
	
	public void delRow(int select) {
		if (tStockAlarmList.size() > 0) {
			if (select >= 0 && select < tStockAlarmList.size()) {
				tStockAlarmList.remove(select);
				fireTableRowsInserted(select, select);
				return;
			}
			tStockAlarmList.remove(tStockAlarmList.size()-1);
			fireTableRowsInserted(tStockAlarmList.size()-1, tStockAlarmList.size()-1);
		}
	}

	//设置单元格是否可编辑
	@Override
	public boolean isCellEditable(int rowIndex,int columnIndex){
		if(columnIndex==0){
			return false;
		}
		return true;
	}
	public void removeData(int row){
		tStockAlarmList.remove(row);
	}
	
	//设置单元格的值
	public void setValueAt(Object aValue,int rowIndex,int columnIndex){
		TStockAlarm tStockAlarm = tStockAlarmList.get(rowIndex);
		switch (columnIndex) {
			case 0:
				return;
			case 1:
				if(aValue!=null){
					if(aValue instanceof TCommodity){
						TCommodity comm=(TCommodity)aValue;
						tStockAlarm.setTCommodity(comm);
					}
				}
				return;
			case 2:
				if(aValue!=null){
					if(aValue instanceof TCommodity){
						TCommodity comm=(TCommodity)aValue;
						tStockAlarm.setTCommodity(comm);
					}
				}
				return;
			case 3:
				if(aValue!=null&&!aValue.toString().equals("")&&MatcherFormatUtil.MatcherNumberScheme(aValue.toString())){
					tStockAlarm.setMinWalve(Integer.parseInt(aValue.toString()));
				}
				return;
			case 4:
				if(aValue!=null&&!aValue.toString().equals("")&&MatcherFormatUtil.MatcherNumberScheme(aValue.toString())){
					tStockAlarm.setMaxWalve(Integer.parseInt(aValue.toString()));
				}
				return;
		}
		super.setValueAt(aValue, rowIndex, columnIndex);
	}
	
	public List<TStockAlarm> getTStockAlarmList() {
		return tStockAlarmList;
	}

}
